package session

import (
	"errors"
	"github.com/satori/go.uuid"
	"sync"
)

// 管理所有的session
type MemorySessionMgr struct {
	sessionMap map[string]Session
	rwlock sync.RWMutex
}

// 构造函数
func NewMemorySessionMgr() *MemorySessionMgr {
	return &MemorySessionMgr{
		sessionMap: make(map[string]Session, 1024),
	}
}

func (m *MemorySessionMgr)Init(addr string, options ...string)(err error){
	return
}

func (m *MemorySessionMgr )CreateSession()(session Session, err error){
	m.rwlock.Lock()
	defer m.rwlock.Unlock()
	// go get github.com/satori/go.uuid
	id := uuid.NewV4()
	sessionId := id.String()
	// 创建一个session
	memorySession := NewMemmorySession(sessionId)
	m.sessionMap[sessionId] = memorySession
	return memorySession, err
}
func (m *MemorySessionMgr)Get(sessionId string)(session Session, err error){
	m.rwlock.Lock()
	defer m.rwlock.Unlock()
	session, ok := m.sessionMap[sessionId]
	if !ok{
		err = errors.New("session not exists")
	}
	return
}